package com.ray.scan;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.drawable.Drawable;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class Setting extends Activity implements OnClickListener {

    private Button flash;
    
    private SharedPreferences sharedPreferences;
    private Editor editor;
    
    private ImageButton left;
    private TextView title;
    private ImageButton right;
    
    private EditText ipEdit;
    private Button setIpOK;
    private EditText programEdit;
    private Button setProgramOK;
    
    private Drawable flashDrawable;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set);
        
        flash = (Button) findViewById(R.id.flash);
        left = (ImageButton) findViewById(R.id.left);
        title = (TextView) findViewById(R.id.title);
        right = (ImageButton) findViewById(R.id.right);
        left.setVisibility(View.VISIBLE);
        title.setText("设置");
        right.setVisibility(View.INVISIBLE);
        left.setOnClickListener(this);
        flash.setOnClickListener(this);
        
        ipEdit = (EditText) findViewById(R.id.ipEdit);
        setIpOK = (Button) findViewById(R.id.setIpOK);
        setIpOK.setOnClickListener(this);
        
        programEdit = (EditText) findViewById(R.id.programEdit);
        setProgramOK = (Button) findViewById(R.id.setProgramOK);
        setProgramOK.setOnClickListener(this);
        
        sharedPreferences = getSharedPreferences("scan", 
                Activity.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        if(sharedPreferences.getString("flash_status", "").equals(Parameters.FLASH_MODE_TORCH)){
            flashDrawable = getResources().getDrawable(R.drawable.flash_on);
        }else{
            flashDrawable = getResources().getDrawable(R.drawable.flash_off);
        }
        flashDrawable.setBounds(0, 0, 
                flashDrawable.getMinimumWidth(), flashDrawable.getMinimumHeight()); 
        flash.setCompoundDrawables(null, null, flashDrawable, null);
        
        if(!sharedPreferences.getString("ip", "").equals("")){
            ipEdit.setText(sharedPreferences.getString("ip", ""));
        }
        
        if(!sharedPreferences.getString("program", "").equals("")){
            programEdit.setText(sharedPreferences.getString("program", ""));
        }
    }

    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        switch (view.getId()) {
        case R.id.left:
            Setting.this.finish();
            break;
            
        case R.id.flash:
            if(sharedPreferences.getString("flash_status", "").equals(Parameters.FLASH_MODE_TORCH)){
                flashDrawable = getResources().getDrawable(R.drawable.flash_off);
                editor.putString("flash_status", Parameters.FLASH_MODE_OFF);
            }else{
                flashDrawable = getResources().getDrawable(R.drawable.flash_on);
                editor.putString("flash_status", Parameters.FLASH_MODE_TORCH);
            }
            flashDrawable.setBounds(0, 0, 
                    flashDrawable.getMinimumWidth(), flashDrawable.getMinimumHeight()); 
            flash.setCompoundDrawables(null, null, flashDrawable, null);
            editor.commit(); 
            break;

        case R.id.setIpOK:
            if(ipEdit.getText() != null){
                editor.putString("ip", ipEdit.getText().toString());
                editor.commit();
                Toast.makeText(Setting.this, 
                        "配置服务器IP地址为" + sharedPreferences.getString("ip", ""), 
                        Toast.LENGTH_SHORT).show();
            }
            break;

        case R.id.setProgramOK:
            if(programEdit.getText() != null){
                editor.putString("program", programEdit.getText().toString());
                editor.commit();
                Toast.makeText(Setting.this, 
                        "配置项目名为" + sharedPreferences.getString("program", ""), 
                        Toast.LENGTH_SHORT).show();
            }
            break;
            
        default:
            break;
        }
    }

}
